<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>
<c:if test="${sessionScope.user != null}">
<script type="text/javascript">
    var dataGrid1;

   $(function(){

	   dataGrid1 = $("#role_datagrid_list").datagrid({
			url : '${ctx}/role/role_datagridlist.do',
			pagination : true,
			fit : true,
			border : true,
			idField : 'id',
			pageSize : 10,
			rownumbers : true,
			pageList : [ 10, 20, 30, 50, 100 ],
			sortName : 'text',
			sortOrder : 'desc',
			checkOnSelect : true,
			selectOnCheck : true,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 150,
				checkbox : true
			}, {
				field : 'text',
				title : '角色名称',
				width : 150,
				sortable : true
			}, {
				field : 'description',
				title : '描述',
				width : 150,
				align : 'right',
				sortable : true
			} , {
				field : 'createdatetime',
				title : '创建时间',
				width : 150,
				align : 'right',
				sortable : true
			} , {
				field : 'updatedatetime',
				title : '修改时间',
				width : 150,
				align : 'right',
				sortable : true
			} ] ],
			toolbar : [ {
				text : '添加',
				iconCls : 'icon-add',
				handler : function() {
					$("#roleadd_dialog").dialog('open');
					$("#roleadd_Form input").val('');
				}
			}, '-', {
				text : '编辑',
				iconCls : 'icon-edit',
				handler : function() {
					role_edit();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					role_remove();
				}
			}, '-', {
				text : '资源配置',
				iconCls : 'icon-remove',
				handler : function() {
					resource_config();
				}
			} ]
		});


   });

	function role_edit(){
		var datagrid = $("#role_datagrid_list");
		var rows = datagrid.datagrid('getChecked');
		var ids = "";
		if(rows.length == 0){
			$.messager.show({
				title:'提示',
				msg:'请您选择一条数据来做编辑操作！'
			});
		}
		if(rows.length > 1){
			$.messager.show({
				title:'提示',
				msg:'只能选择一条数据来做编辑操作！'
			});
		}
		if(rows.length == 1){
			var dynamic_dialog  = $("<div/>").dialog({
			    title: '修改',
			    width: 550,
			    height: 200,
			    closed: false,
			    cache: false,
			    href: '${ctx}/role/editTo.do',
			    modal: true,
			    buttons:[{
					text : '保存',
					iconCls: 'icon-add',
					handler : function() {
						$.serializeObject($('#roleEdit_edit_form'));
						$('#roleEdit_edit_form').form('submit',{
							url:'${ctx}/role/edit.do',
							onSubmit:function(){
							},
							success:function(data){
								var obj = $.parseJSON(data);
								if(obj.success){
									dynamic_dialog.dialog('close');
									datagrid.datagrid('updateRow',{
										index: datagrid.datagrid('getRowIndex',rows[0]),	// index start with 0,在第一行插入数据
										row: obj.obj
									});
								}
								$.messager.show({
									title:'提示',
									msg:obj.msg
								});
							}
						});
					}
				} ],
				onClose:function(){
					$(this).dialog('destroy');
				},
				onLoad:function(){
					$("#roleEdit_edit_form").form('load',rows[0]);
				}

			});
		}
	}

	function role_remove() {
		var datagrid = $("#role_datagrid_list");
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if(rows.length > 0){
			$.messager.confirm('确认','确认要要删除吗？',function(r){
			    if (r){
			    	for(var i=0;i<rows.length;i++){
						ids.push(rows[i].id);
					}
					$.ajax({
						url:'${ctx}/role/del.do',
						data:{
							ids:ids.join(',')
						},
						dataType:'json',
						success:function(data){
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');//使全部的行形成未選中狀態
							$.messager.show({
								title:'提示',
								msg:data.msg
							});
						}
					});
			    }
			});
		}else{
			$.messager.show({
				title:'提示',
				msg:'请至少选择一行来做删除操作！'
			});
		}
	}

	function resource_config(){
		var datagrid = $("#role_datagrid_list");
		var rows = datagrid.datagrid('getChecked');
		var ids = [];

			if(rows.length > 0){
				parent.$.modalDialog({
				    title: '角色资源配置',
				    width: 550,
				    height: 500,
				    closed: false,
				    cache: false,
				    href: '${ctx}/role/menuTreeTo.do?id='+rows[0].id,
				    modal: true,
				    buttons:[{
						text : '设置',
						iconCls: 'icon-add',
						handler : function() {
							parent.$.modalDialog.openner_dataGrid = dataGrid1;//因为添加成功之后，需要刷新这个datagrid，所以先预定义好
							var f = parent.$.modalDialog.handler.find('#form');
							f.submit();
						}
					} ]
				});
		}else{
			$.messager.show({
				title:'提示',
				msg:'请至少选择一行来做资源配置操作！'
			});
		}
	}
</script>

<div class="easyui-layout" data-options="fit : true,border : false">
	<div data-options="region:'center',border:false">
		<table id="role_datagrid_list"></table>
	</div>
</div>
<div id="roleadd_dialog" class="easyui-dialog"
	style="width: 550px;height:200px;"
	data-options="closed:true,modal:true,title:'添加角色',buttons:[{
				text : '添加',
				iconCls: 'icon-add',
				handler : function() {

					$('#roleadd_Form').form('submit',{
						url:'${ctx}/role/save.do',
						onSubmit:function(){
						    $.serializeObject($('#roleadd_Form'));
						    return $('#roleadd_Form').form('validate');
						},
						success:function(data){
							var obj = $.parseJSON(data);
							if(obj.success){
								$('#role_datagrid_list').datagrid('load')//此方法还得发送一次查询的请求效率较低，所以要用下面的方法替代
								$('#roleadd_dialog').dialog('close');
							}
							$.messager.show({
								title:'提示',
								msg:obj.msg
							});
						}
					});
				}
			} ]">
	<form id="roleadd_Form" method="post">
		<table>
			<tr>
					<th>角色名称</th>
					<td><input name="text" type="text" placeholder="请输入登录名称" class="easyui-validatebox span2" data-options="required:true" value=""></td>
					<th>描述</th>
					<td><input name="description" type="text" placeholder="请输入描述" class="easyui-validatebox span2"></td>
				</tr>
		</table>
	</form>
</div>
</c:if>
<c:if test="${sessionScope.user == null}">
			<img src="${ctx}/style/images/blue_face/bluefaces_35.png" alt="" />
			<div>登录已超时，请刷新后重新登录，！</div>
</c:if>

